/**
 * 
 */
package simulation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;

//import org.jdom.input.SAXBuilder;
//import org.w3c.dom.*;

/**
 * @author aclayton
 *
 */
public class Simulation {
	private Map simMap;
	private HashSet<Robot> robot;
	private Configuration start, goal;
	private ConfigGraph graph;
	private Path path;
	
	public Simulation(Map map){
		simMap = map;
		robot = new HashSet<Robot>();
		graph = null;
		path = null;
	}
	
	public Simulation(double width, double length){
		simMap = new Map(width, length);
		robot = new HashSet<Robot>();
		graph = null;
		path = null;
	}
	
	public Map getMap(){
		return simMap;
	}
	
	public Configuration getStart(){
		return start;
	}
	
	public Configuration getGoal(){
		return goal;
	}

	/**
	 * @return the graph
	 */
	public ConfigGraph getGraph() {
		return graph;
	}

	/**
	 * @param graph the graph to set
	 */
	public void setGraph(ConfigGraph graph) {
		this.graph = graph;
	}

	/**
	 * @return the path
	 */
	public Path getPath() {
		return path;
	}

	/**
	 * @param path the path to set
	 */
	public void setPath(Path path) {
		this.path = path;
	}
	
	/**
	 * @return the robot
	 */
	public HashSet<Robot> getRobot() {
		return robot;
	}

	/**
	 * @param robot the robot to set
	 */
	public void setRobot(HashSet<Robot> robot) {
		this.robot = robot;
	}
	
	public void addRobot(Robot r){
		this.robot.add(r);
	}
	
	public boolean robotExists(int num){
		for(Robot r : robot){
			if(r.getRobotNumber() == num)
				return true;
		}
		return false;
	}
	
	public Robot getRobotNum(int num){
		for(Robot r : robot){
			if(r.getRobotNumber() == num)
				return r;
		}
		return null;
	}
	
	public void createRobots(int num, double radius){
		int currentNum = 0;
		for(int a = 0; a < num; a++){
			while(robotExists(currentNum))
				currentNum++;
			
			robot.add(new Robot(currentNum, radius));
		}
		
	}
	

	public void LoadFromFile(String string) {
		robot = new HashSet<Robot>();
		graph = null;
		path = null;
		
		
		Scanner sc = null;
		try {
			sc = new Scanner(new File(string));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String temp;
		String[] strs;
		int width = 10, length = 10;
		Map map;
		ArrayList<Obstacle> obs = new ArrayList<Obstacle>();
		ArrayList<Robot> robs = new ArrayList<Robot>();
		char tempchar;
		boolean notdone = true;
		try{
			temp = sc.nextLine();
			simMap.loadFromFile(temp);
			
			while(notdone && sc.hasNextLine()){
				temp = sc.nextLine();
				
				if(temp.length()<2)
					continue;
				
				tempchar = temp.charAt(0);
				
				strs = temp.split("=");
				
				switch(tempchar){
				case 'R':
					Robot r = Robot.createFromString(temp);
					robot.add(r);
					break;
				case 'C':
					notdone = false;
					break;
				}
			}
			
			notdone = true;
			String config = temp;
			while(notdone && sc.hasNextLine()){
				temp = sc.nextLine();
				config = config  + '\n' + temp;
				if(temp.charAt(0) == 'C' && temp.charAt(1) == 'o')
					notdone = false;
			}
			
			start = Configuration.CreateFromString(this, config);
			
			
			notdone = true;
			temp = sc.nextLine();
			config = temp;
			while(notdone && sc.hasNextLine()){
				temp = sc.nextLine();
				config = config +  '\n' + temp;
				if(temp.charAt(0) == 'C' && temp.charAt(1) == 'o')
					notdone = false;
			}
			
			goal = Configuration.CreateFromString(this, config);
			
		} catch(Exception e) {
			e.printStackTrace();
			sc.close();
			System.exit(0);
		}
		sc.close();
	}
}
